<?php
namespace App\Library\Impl;
use Illuminate\Support\Facades\Log;
use App\Library\UserLibraryInterface;
use App\Models\DawnUser;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use App\Pojo\ResponsePojo;

class UserLibraryImpl implements UserLibraryInterface{
    /**
     * @param mixed $credentials
     * @return int
     */
    public function register($credentials){
        $saveStatus = False;
        $user = $this->findByUsername($credentials['username']);
        if(!$user){
            $newUser = new DawnUser();
            $newUser->dawn_name = $credentials['username'];
            $newUser->dawn_password = Hash::make($credentials['password']);
            $newUser->dawn_createTime = Carbon::now();
            $newUser->dawn_lastTime = Carbon::now();
            $newUser->dawn_updatatime = Carbon::now();
            $newUser->dawn_Status = 1;
            $saveStatus = $newUser->save();
            $saveStatus = True;
        }else{
            $saveStatus = False;
        }
        return $saveStatus;
    }

    // 实现注册逻辑
    /**
     * 根据username 查找用户
     */
    public function findByUsername($username){
        return DawnUser::where("dawn_name", $username)->first();
    }

    // 实现注册逻辑
    /**
     * 创建用户
     */
    public function insertUser($data){
        // return DawnUser::insert();
    }
}
